package cn.itcast.scala.course06

import scala.util.Random

/**
  * create by zhangtian on 2019/1/8
  */
object MatchApp extends App {
  val names = Array("AAA", "BBB", "CCC", "DDD", "EEE") ;
  val name = names(Random.nextInt(names.length))

  // 简单的模式匹配
  println("************** 简单的模式匹配 *************")
  name match {
    case "AAA" => print("A老师")
    case "BBB" => print("B老师")
    case "CCC" => print("C老师")
    case "DDD" => print("D老师")
    case "EEE" => print("E老师")
    case _ => print("NO老师")
  }

  println()
  def judgeGrade(grade: String): String ={
    var result = ""
    grade match {
      case "A" => result = "优秀"
      case "B" => result = "良好"
      case "C" => result = "合格"
      case "D" => result = "不合格"
      case _ => result = "非法数据"
    }
    result
  }

  println(judgeGrade("A"))
  println(judgeGrade("C"))
  println(judgeGrade("W"))

  println("************** 带条件判断的模式匹配 *************")
  def judgeNameGrade(name: String, grade: String): String ={
    var result = ""
    grade match {
      case "A" => result = "优秀"
      case "B" => result = "良好"
      case "C" => result = "合格"
      case "D" => result = "不合格"
      case _ if(name == "lisi") => result = name + ",加油哦，小伙!" // 双重过滤
      case _ => result = "非法数据"
    }
    result
  }

  println(judgeNameGrade("zhangsan", "A"))
  println(judgeNameGrade("lisi", "C"))
  println(judgeNameGrade("lisi", "W"))

  println("************** 数组的模式匹配 *************")
  def greeting(array: Array[String]): Unit = {
    array match {
      case Array("zhangsan") => println("Hi, zhangsan")
      case Array(x, y) => println("Hi, " + x + ", "+ y)
      case Array("zhangsan", _*) => println("Hi, zhangsan and other friends")
      case _ => println("Hi, everybody")
    }
  }

  greeting(Array("zhangsan"))
  greeting(Array("zhangsan", "lisi"))
  greeting(Array("zhangsan", "lisi", "wangqu"))
  greeting(Array("lisi", "zhangsan"))
  greeting(Array("lisi", "zhangsan", "other"))

  println("************** List的模式匹配 *************")
  def greetingList(list: List[String]): Unit = {
    list match {
      case "zhangsan" :: Nil => println("Hi, zhangsan")
      case x :: y :: Nil => println("Hi, " + x + ", "+ y)
      case "zhangsan" :: tail => println("Hi, zhangsan and other friends")
      case _ => println("Hi, everybody")
    }
  }

  greetingList(List("zhangsan"))
  greetingList(List("zhangsan", "lisi"))
  greetingList(List("zhangsan", "lisi", "wangqu"))
  greetingList(List("lisi", "zhangsan"))
  greetingList(List("lisi", "zhangsan", "other"))

  println("************** 类型的模式匹配 *************")
  def matchType(obj: Any): Unit = {
    obj match {
      case obj: Int => println("Int")
      case obj: String => println("String")
      case obj: Map[_, _] => obj.foreach(println)
      case _ => println("other type ......")
    }
  }

  matchType(1)
  matchType("1")
  matchType(1f)
  matchType(Map("name"->"PK"))

  println("************** case class的模式匹配 *************")
  class Person
  case class CTO(name: String, floor: String) extends Person
  case class Employee(name: String, floor: String) extends Person
  case class Other(name: String) extends Person

  def caseclassMatch(person: Person): Unit = {
    person match {
      case CTO(name, floor) => println("CTO name is: "+ name +", working floor is: "+floor)
      case Employee(name, floor) => println("Employee name is: "+ name +", working floor is: "+floor)
      case Other(name) => println("Other...")
    }
  }

  caseclassMatch(CTO("Mike", "22"))
  caseclassMatch(Employee("Jack", "2"))
  caseclassMatch(Other("Jack"))

  println("************** Some None Option的模式匹配 *************")
  // Some表示有  None表示没有
  val grades = Map("PK"->"A", "MIKE"->"C")

  def getGrade(name: String): Unit = {
    val grade = grades.get(name) ;
    grade match {
      case Some(grade) => println(name + ": your grade is " +grade)
      case None => println(name + ": sorry ...... ")
    }
  }

  getGrade("MIKE")
  getGrade("PK")
  getGrade("JACK")
}
